/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.data.provider.postgis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author yangjiandong
 * 
 */
public class PostgisConnection {

	private String drive = "org.postgresql.Driver";
	private String url = "jdbc:postgresql://localhost:5432/postgres";
	private String userName = "postgres";
	private String passWord = "123";
	
	public Connection dbcon;

	public PostgisConnection(String drive, String url, String userName,
			String passWord) {
		super();
		this.drive = drive;
		this.url = url;
		this.userName = userName;
		this.passWord = passWord;
	}
	
	//-----------------------------------------------------------------------
	
	private Connection getConnection() throws ClassNotFoundException,
			SQLException {
		Class.forName(drive);
		Connection connection = DriverManager.getConnection(url, userName,
				passWord);
		
		return connection;
	}
	
	public void open()
    {
        try {
			dbcon = getConnection();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }

    public void close()
    {
        try {
			dbcon.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }

	public ResultSet executeScalar(String sql) {
		ResultSet r=null;
		try {
			r = dbcon.createStatement().executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return r;
	}
}